package com.example.springdemo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springdemo.annotation.RestResponse;
import com.example.springdemo.domain.base.BasePageReq;
import com.example.springdemo.domain.reqVo.ReqAddUser;
import com.example.springdemo.domain.reqVo.ReqUpdateUser;
import com.example.springdemo.domain.table.User;
import com.example.springdemo.mapstruct.IUserMap;
import com.example.springdemo.service.interfaces.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * HelloController
 *
 * @author wenguang
 * @date 2020/11/17 15:37
 */
@RestResponse
@RestController
@RequestMapping("user")
@Api(value = "user", tags = "用户")
@CacheConfig(cacheNames = {"demo:user"})
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserController {

    private final UserService userService;

    private final IUserMap userMap;

    @PostMapping("/getList")
    public Page<User> getList(@RequestBody @Validated BasePageReq<User> pageReq) {
//        Page<User> page = userService.page(pageReq.getPage(), new QueryWrapper<>(pageReq.getData()));
        Page<User> page = userService.query()
            .setEntity(pageReq.getData())
            .page(pageReq.getPage());
        return page;
    }

//    @Caching(
//        cacheable = {
//            @Cacheable(key = "targetClass + methodName + #p0", sync = true)
//        },
//        put = {
//            @CachePut(key = "targetClass + methodName + #p0")
//        }
//    )
    @Cacheable(key = "targetClass + methodName + #p0", unless = "#result == null")
    @GetMapping("/get")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, required = true),
    })
    public User get(@RequestParam("id") @NotNull(message = "id不能为空") Integer id) {
        User user = userService.getById(id);
        return user;
    }

    @CacheEvict
    @PostMapping("/add")
    public Boolean add(@RequestBody @Validated ReqAddUser request) {
        User user = userMap.toUserDo(request);
        boolean success = userService.save(user);
        return success;
    }

    @CacheEvict(allEntries = true)
    @PutMapping("/update")
    public Boolean update(@RequestBody @Validated ReqUpdateUser request) {
        User user = userMap.toUserDo(request);
        boolean success = userService.updateById(user);
        return success;
    }

    @CacheEvict(key = "targetClass + methodName + #p0")
    @DeleteMapping("/delete/{id}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, paramType = "path", required =
            true),
    })
    public Boolean delete(@PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        boolean success = userService.removeById(id);
        return success;
    }

}
